<template>
  <div>
    <!-- 1.0 面包屑组件 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>栏目列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片组件 -->
    <el-card class="box-card">
      <el-button type="success" class="btn" @click="dialogFormVisible = true"
        >添加栏目
      </el-button>
      <el-dialog title="添加栏目" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" class="eldialog">
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input v-model.trim="form.name"> </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
      </el-dialog>
      <template>
        <el-table :data="categroList" border style="width:90%">
          <el-table-column prop="title" label="编号" type="index" width="100">
          </el-table-column>
          <el-table-column label="栏目名称">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
  </div>
</template>

<script>
import { getCategoryAPI, PostCategoryAPI } from "@/apis/user";
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {
        name: "",
      },
      formLabelWidth: "80px",
      categroList: [],
    };
  },
  async created() {
    this.getCategoryID();
  },
  methods: {
    async getCategoryID() {
      const res = await getCategoryAPI();
      // console.log(res);
      this.categroList = res.data.data;
    },

    async onSubmit() {
      // console.log("点击了", this.form.name);
      const data = {
        ...this.form,
      };
      // console.log(data);
      if (this.form.name === "")
        return this.$message.warning("栏目昵称不能为空");
      const res = await PostCategoryAPI(data);
      console.log(res);
      if (res.data.message === "栏目添加成功") {
        this.$message.warning("栏目添加成功");
        this.form.name = "";
        this.dialogFormVisible = false;
        this.getCategoryID();
      } else {
        this.$message.warning("请检查参数");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.box-card {
  margin-top: 15px;
}
.btn {
  margin-bottom: 10px;
}
</style>
